<template>
  <div class="main-container">
    <div class="login_container">
      <div class="content_container">
        <!-- 登录图片 -->
        <div class="content_image">
          <el-image
            src="https://www.wangluotongxin.top/word/file/core/admin_index.jpg"
          >
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
        </div>
        <!-- 登录信息 -->
        <div class="content_login" @keyup.native.enter="login">
          <el-form :model="loginForm" ref="loginForm" :rules="rules">
            <el-tabs v-model="activeName" @tab-click="tabsClick">
              <el-tab-pane label="账号" name="user">
                <el-form-item :prop="activeName === 'user' ? 'username' : ''">
                  <el-input
                    v-model="loginForm.username"
                    placeholder="请输入账号"
                    suffix-icon="el-icon-user"
                  />
                </el-form-item>
                <el-form-item :prop="activeName === 'user' ? 'password' : ''">
                  <el-input
                    type="password"
                    v-model="loginForm.password"
                    placeholder="请输入密码"
                    show-password
                    suffix-icon="el-icon-lock"
                  />
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="手机" name="phone">
                <el-form-item :prop="activeName === 'phone' ? 'phone' : ''">
                  <el-input
                    v-model="loginForm.phone"
                    maxlength="11"
                    placeholder="请输入手机号"
                    suffix-icon="el-icon-mobile-phone"
                  />
                </el-form-item>
                <el-form-item prop="code">
                  <div class="login_input_code">
                    <el-input
                      v-model="loginForm.code"
                      maxlength="4"
                      placeholder="请输入验证码"
                    />
                    <el-button
                      type="text"
                      :disabled="codeBtn.disabledCodeBtn"
                      @click="getPhoneCode"
                      >{{ codeBtn.codeBtnName }}</el-button
                    >
                  </div>
                </el-form-item>
              </el-tab-pane>
            </el-tabs>
          </el-form>
          <!-- 记住账号/手机号 -->
          <div class="remember_user_phone">
            <el-checkbox v-model="isRemember"
              >记住{{ activeName === "phone" ? "手机号" : "账号" }}</el-checkbox
            >
          </div>
          <!-- 登录-->
          <div class="login_btn">
            <el-button
              type="primary"
              round
              style="width: 100%"
              :loading="loading"
              @click.native.prevent="login"
              >登录</el-button
            >
          </div>
        </div>
      </div>
    </div>
    <div class="icp-container">
      <span style="margin-right: 10px">个人技术分享</span>
      <span style="margin-right: 10px">电话：13628610467</span>
      <a href="https://beian.miit.gov.cn/" target="_blank" underline="none"
        >豫ICP备2024041443号</a
      >
    </div>
  </div>
</template>

<script>
import SocketConfig from "@/socket/config";

import { phoneCode } from "@/api/user";
import { set } from "js-cookie";

export default {
  name: "login",
  data() {
    return {
      activeName: "user",
      isRemember: false,
      loading: false,
      loginForm: {
        username: null,
        password: null,
        phone: null,
        code: null,
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
      codeBtn: {
        codeBtnName: "点击发送",
        disabledCodeBtn: false,
        codeGetTime: 60,
        codeTimeContol: 0,
      },
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    document.onkeydown = (e) => {
      if (e.key === "Enter" && this.$route.fullPath.match(/login/)) {
        this.login();
      }
    };
    const username = this.$storage.getRememberUser();
    if (username) {
      this.isRemember = true;
      this.loginForm.username = username;
    }
  },
  methods: {
    tabsClick() {
      if (this.activeName === "user") {
        const username = this.$storage.getRememberUser();
        if (username) {
          this.isRemember = true;
          this.loginForm.username = username;
        } else {
          this.isRemember = false;
          this.loginForm.username = null;
        }
      } else {
        const phone = this.$storage.getRememberPhone();
        if (phone) {
          this.isRemember = true;
          this.loginForm.phone = phone;
        } else {
          this.isRemember = false;
          this.loginForm.phone = "";
        }
      }
    },

    // 获取验证码
    async getPhoneCode() {
      if (!this.loginForm.phone) {
        this.$message.error("请输入手机号");
        return false;
      }
      this.$storage.setRememberPhone(this.loginForm.phone);
      const res = await phoneCode({ phone: this.loginForm.phone });
      if (res) {
        this.$message.success("验证码发送成功");
        // 开始定时
        this.codeBtn.codeTimeContol = this.codeBtn.codeGetTime;
        let timerId = setInterval(() => {
          this.codeBtn.disabledCodeBtn = true;
          this.codeBtn.codeBtnName = this.codeBtn.codeTimeContol + "s重新获取";
          if (this.codeBtn.codeTimeContol < 1) {
            this.codeBtn.codeBtnName = "重新获取";
            this.codeBtn.disabledCodeBtn = false;
            this.codeBtn.codeTimeContol = 0;
            clearInterval(timerId);
          }
          this.codeBtn.codeTimeContol--;
        }, 1000);
      }
    },

    // 登录
    login() {
      if (this.activeName === "user") {
        this.userLogin();
      } else {
        this.phoneLogin();
      }
      if (this.isRemember) {
        if (this.activeName === "user") {
          this.$storage.setRememberUser(this.loginForm.username);
        }
        if (this.activeName === "phone") {
          this.$storage.setRememberPhone(this.loginForm.phone);
        }
      } else {
        if (this.activeName === "user") {
          this.$storage.removeRememberUser();
        }
        if (this.activeName === "phone") {
          this.$storage.removeRememberPhone();
        }
      }
    },

    // 账号密码登录
    userLogin() {
      console.log(this.activeName);
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
              setTimeout(() => {
                const message = {
                  msgType: SocketConfig.msgType.login,
                  clientId: this.$store.getters.info.id,
                  dateTime: this.$moment().format("YYYY-MM-DD HH:mm:ss"),
                };
                this.$store.getters.websocket.send(JSON.stringify(message));
              }, 1000);
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 手机号登录
    phoneLogin() {
      alert(0);
    },
  },
};
</script>

<style lang="scss" scoped>
.main-container {
  width: 100%;
  height: 100%;
  margin-left: 0px !important;
  display: flex;
  justify-content: center;

  .login_container {
    padding: 0px;
    margin: auto 100px;
    width: 1100px;
    height: 700px;
    overflow: hidden;

    .content_container {
      margin: auto 30px;
      background-color: white;
      border: 1px solid #dddddd;
      position: relative;
      overflow: hidden;
      display: flex;
      justify-content: flex-start;

      .content_image {
        width: 550px;
        height: 700px;

        .el-image {
          height: 100%;
        }
      }

      .content_login {
        margin: 200px auto;

        ::v-deep .el-tabs__header {
          width: 300px;
          display: flex;
          justify-content: center;

          .el-tabs__item {
            font-size: 25px;
          }

          #tab-phone {
            margin-left: 50px;
          }
        }

        /*去掉切换时el-tab-pane底部的蓝色下划线*/
        ::v-deep .el-tabs__active-bar {
          background-color: transparent !important;
        }
        /*去掉tabs底部的下划线*/
        ::v-deep .el-tabs__nav-wrap::after {
          position: static !important;
        }

        ::v-deep .el-tab-pane {
          margin-top: 40px;
        }

        ::v-deep .el-input__inner {
          border-left: hidden;
          border-top: hidden;
          border-right: hidden;
          border-radius: 0px;
          margin-bottom: 10px;
          margin-top: 10px;
        }

        ::v-deep .login_input_code {
          width: 100%;
          position: relative;

          .el-input__inner {
            width: 70%;
          }

          .el-button {
            border-bottom: 1px solid #dcdfe6;
            width: 30%;
            position: absolute;
            left: 69%;
            top: 15%;
          }
        }

        .login_btn {
          width: 100%;
          margin-top: 20px;

          .el-button {
            font-size: 20px;
          }
        }
      }
    }
  }

  .icp-container {
    position: fixed;
    top: calc(100vh - 50px);
    color: #a7aab5;

    a {
      text-decoration: none;
      color: #a7aab5;
    }

    a:hover {
      color: black;
    }
  }
}
</style>
